package com.znwxss.config;


import cn.dev33.satoken.interceptor.SaInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * saToken配置
 * @Author: hb
 * @CreateTime: 2023-12-25 06:20
 * @Version: 1.0
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/files/**")
//                .addResourceLocations("file:" + "/mydata/pic/");
//    }

    /**
     * 拦截器鉴权
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

            // 注册 Sa-Token 拦截器，打开注解式鉴权功能
            registry.addInterceptor(new SaInterceptor())
                    .addPathPatterns("/**")
                    .excludePathPatterns("/login/**")
                    .excludePathPatterns("/sms/**");
    }


    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .maxAge(3600);
    }

//    /**
//     * 扩展mvc框架的消息转换器
//     */
//    public void extendMessageConverters(List<HttpMessageConverter<?>> converters){
//        //创建消息转换器对象
//        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
//        //设置对象转换器,底层使用jackson将java对象转为json
//        messageConverter.setObjectMapper(new JacksonObjectMapper());
//        //将上面的消息转换器追加到mvc框架的转换器集合中
//        converters.add(0,messageConverter);
//    }

}
